<template>
  <div>
    <el-row style="margin: 10px">
      <el-col>
        <el-col :span="4" style="margin-left: 20px">
          <el-input v-model="searchTitle" placeholder="通过消息内容搜索" clearable maxlength="20" />
        </el-col>
        <el-col :span="1" style="margin-left: 20px">
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </el-col>
        <el-col :span="1" style="margin-left: 20px">
          <el-button type="primary" @click="handleAdd()">添加</el-button>
        </el-col>
      </el-col>
    </el-row>
    <el-table :data="listData" style="width: 100%">
      <el-table-column label="接收人id" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.receiverId === -1" style="margin-left: 10px">所有用户</span>
          <span v-else style="margin-left: 10px">{{ scope.row.receiverId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消息内容" style="width: 300px" align="center">
        <template slot-scope="scope">
          <span v-if="!scope.row.message">无</span>
          <el-popover v-else trigger="hover" placement="top">
            <p>内容: {{ scope.row.message }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.message.length>10?scope.row.message.substring(0,20)+'...':scope.row.message }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" width="240px" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px"><el-date-picker type="datetime" :value="scope.row.createTime" readonly /></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!--          <el-button size="mini" type="text" @click="handleEdit(scope.$index, scope.row)">修改</el-button>-->
          <el-button size="mini" type="text" style="color: #f6416c" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="12" :offset="14" style="margin-top: 20px">
      <el-pagination
        background
        layout="prev, pager, next, jumper, sizes"
        :current-page="page.currentPage"
        :total="page.total"
        :pager-count="5"
        :page-size="page.pageSize"
        :page-sizes="page.pageSizes"
        @current-change="pageChange"
        @size-change="sizeChange"
      />
    </el-col>
    <el-dialog title="添加消息" :visible.sync="dialogVisible" width="30%" :rules="rules">
      <el-form ref="messageForm" :model="messageForm" :rules="rules" label-position="right">
        <el-form-item label="接收人id（-1代表全部）：" prop="receiverId">
          <el-input v-model="messageForm.receiverId" type="number" />
        </el-form-item>
        <el-form-item label="消息内容：" prop="message">
          <el-col>
            <el-input v-model="messageForm.message" type="textarea" :autosize="{ minRows: 4, maxRows: 6}" maxlength="500" />
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleConfirm('messageForm')">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="添加消息" :visible.sync="editDialogVisible" width="30%" :rules="rules">
      <el-form ref="messageForm" :model="messageForm" :rules="rules" label-position="right">
        <el-form-item label="接收人id（-1代表全部）：" prop="receiverId">
          <el-input v-model="messageForm.receiverId" type="number" />
        </el-form-item>
        <el-form-item label="消息内容：" prop="message">
          <el-col>
            <el-input v-model="messageForm.message" type="textarea" :autosize="{ minRows: 4, maxRows: 6}" maxlength="5" />
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleUpdate('messageForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getListByPage, addSystemMessage, updateSystemMessage, deleteSystemMessage } from '@/api/systemMessage'

export default {
  data() {
    return {
      listData: [],
      searchTitle: '',
      dialogVisible: false,
      editDialogVisible: false,
      fileLists: [],
      page: {
        pageSize: 5,
        pageSizes: [5, 10, 20, 40, 80],
        totalPage: 1,
        total: 0,
        currentPage: 1
      },
      messageForm: {
        id: '',
        receiverId: -1,
        message: ''
      },
      rules: {
        receiverId: [
          { required: true, message: '排序号不能为空' }
        ],
        message: [
          { required: true, message: '请输入消息内容', trigger: 'blur' },
          { min: 4, max: 500, message: '长度在 4 到 500 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getListByPage({
        page: this.page.currentPage,
        limit: this.page.pageSize,
        searchTitle: this.searchTitle
      }).then((res) => {
        console.log(res)
        this.listData = JSON.parse(res.data.list)
        for (let i = 0; i < this.listData.length; i++) {
          if (this.listData[i].img) {
            this.listData[i].showImg = [].concat(res.data.prefixUrl + this.listData[i].img)
          }
        }
        console.log(this.listData)
        this.page.total = res.data.total
        this.page.totalPage = res.data.totalPage
      })
    },
    handleSearch() {
      this.currentPage = 1
      this.getList()
    },
    pageChange(page) {
      this.page.currentPage = page
      this.getList()
    },
    sizeChange(size) {
      this.page.pageSize = size
      this.getList()
    },
    handleCancel() {
      this.dialogVisible = false
      this.editDialogVisible = false
      this.resetForm()
    },
    handleAdd() {
      this.resetForm()
      this.dialogVisible = true
    },
    handleEdit(index, row) {
      this.messageForm = row
      this.editDialogVisible = true
    },
    handleDelete(index, row) {
      deleteSystemMessage(row).then((res) => {
        if (res.code === 200) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getList()
          this.resetForm()
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
          this.resetForm()
        }
      })
    },
    handleUpdate() {
      updateSystemMessage(this.messageForm).then((res) => {
        if (res.code === 200) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.editDialogVisible = false
          this.getList()
          this.resetForm()
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
          this.resetForm()
        }
      })
    },
    handleConfirm(messageForm) {
      this.$refs[messageForm].validate((valid) => {
        if (valid) {
          addSystemMessage(this.messageForm).then((res) => {
            if (res.code === 200) {
              this.$message({
                message: res.message,
                type: 'success'
              })
              this.getList()
              this.resetForm()
            } else {
              this.$message({
                message: res.message,
                type: 'error'
              })
              this.resetForm()
            }
            this.dialogVisible = false
            this.getList()
          })
        } else {
          this.$message({
            message: '格式错误',
            type: 'error'
          })
          return false
        }
      })
    },
    resetForm() {
      this.messageForm.receiverId = -1
      this.messageForm.message = ''
    }
  }
}
</script>

<style scoped>

</style>
